namespace WebApplication1;

public interface IPackageFactory
{
    Type PackageType { get; }

    RpcPackageBase Create();
}

public sealed class RpcPackageFactory<TPackage> : IPackageFactory 
    where TPackage : RpcPackageBase, new()
{
    private const int DefaultMaxCount = 10;

    public RpcPackageFactory()
    {
        PackageType = typeof(TPackage);
    }

    public Type PackageType { get; private set; }

    public RpcPackageBase Create()
    {
        return new TPackage();
    }
}

public sealed class RpcPackageFactoryPool
{
    private readonly IPackageFactory[] _packetFactories = new IPackageFactory[20];

    public RpcPackageFactoryPool()
    {
        RegisterPacketType<LoginRequest>(CommandKey.Login);
        RegisterPacketType<LoginResponse>(CommandKey.LoginAck);
    }

    public IPackageFactory Get(CommandKey command)
    {
        return _packetFactories[(byte)command];
    }

    private void RegisterPacketType<TPackage>(CommandKey command)
        where TPackage : RpcPackageBase, new()
    {
        _packetFactories[(byte)command] = new RpcPackageFactory<TPackage>();
    }
}